package com.ihighest.app.infox.activity;

import android.os.Bundle;
import android.view.View;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.WikiContentListAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.tools.CheckNetWork;

/**
 * 百科内容列表界面
 * 
 * @author Vity Created at 2015-10-22 上午12:53:07
 * @version 1.0
 */
public class WikiContentListActivity extends BaseActivity {

	final String TAG = "WikiContentListActivity";

	ActivityTitle title;
	PullToRefreshListView mRefreshListView;
	WikiContentListAdapter adapter;
	View tvNoResult;
	String id;
	int page = 1;
	long time = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_wiki_content);
		initView();
		loadData();
	}

	/**
	 * 初始化界面
	 * 
	 * 
	 * @author Vity Created at 2015-10-22 上午12:06:47
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void initView() {
		title = (ActivityTitle) findViewById(R.id.activityTitle);
		title.setTitle("内容列表");
		title.setViewVersible(title.titleBack);
		title.setViewGone(title.titleMsg, title.titleRefresh, title.rightText);
		id = getIntent().getStringExtra("id");
		tvNoResult = findViewById(R.id.noresult);
		mRefreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefresh);
		adapter = new WikiContentListAdapter(this);
		mRefreshListView.setAdapter(adapter);
		mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				if (CheckNetWork.isConnected(WikiContentListActivity.this)) {
					page = 1;
					time = -1;
					loadData();
				} else {
					ToastKit.showShort(WikiContentListActivity.this, "请连接网络");
				}
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				if (CheckNetWork.isConnected(WikiContentListActivity.this)) {
					page++;
					loadData();
				} else {
					ToastKit.showShort(WikiContentListActivity.this, "请连接网络");
				}
			}

		});
	}

	/**
	 * 加载数据
	 * 
	 * @author Vity Created at 2015-10-22 上午12:09:45
	 */
	public void loadData() {
		ApiParam param = ApiParam.create();
		param.addParam("id", id);
		param.addParam("page", page);
		if (time >= 0) {
			param.addParam("time", time);
		}

		Api.get("wiki/infos", param, new ApiCallback(this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getDatas() == null || result.getDatas().length() == 0) {
					if (page == 1) {
						tvNoResult.setVisibility(View.VISIBLE);
						mRefreshListView.setVisibility(View.GONE);
					} else {
						tvNoResult.setVisibility(View.GONE);
						mRefreshListView.setVisibility(View.VISIBLE);
						page--;
					}
				} else {
					time = result.getTime();
					tvNoResult.setVisibility(View.GONE);
					mRefreshListView.setVisibility(View.VISIBLE);
					if (page > 1) {
						adapter.appendDatas(result.getDatas());
					} else {
						adapter.setDatas(result.getDatas());
					}
					adapter.notifyDataSetChanged();
				}
				mRefreshListView.onRefreshComplete();
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				page--;
			}
		}, this);

	}
}
